//
//  SportsCircleView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/7/5.
//

import SwiftUI
import URLImage
import ToastUI


struct SportsCircleView: View {
    @Binding var dynamicitem: DynamicItem
    @StateObject var proModel = profileViewModel()
    @State private var isExpanded = false
    @State private var showToast = false
    @State private var isLike: Bool
    init(dynamicitem: Binding<DynamicItem>) {
        _dynamicitem = dynamicitem
        _isLike = State(initialValue: dynamicitem.wrappedValue.liked)
    }
    var body: some View {
        VStack(alignment: .leading ,spacing: 16) {
            // 用户信息栏
            HStack(spacing: 10) {                
                if let urlStr = dynamicitem.avatar,let url = URL(string: urlStr) {
                    URLImage(url: url) { image in
                        image
                            .resizable()
                            .frame(width: 49, height: 49)
                            .clipShape(Circle())
                    }
                }else {
                    Image("21")
                        .resizable()
                        .frame(width: 49, height: 49)
                        .clipShape(Circle())
                }
                
                VStack(alignment: .leading) {
                    Text(dynamicitem.nickname)
                        .font(.system(size: 16))
                        .foregroundStyle(.white)
                    Text(String(dynamicitem.createTime))
                        .font(.system(size: 14))
                        .foregroundStyle(Color(hex: "#8A8A8A"))
                }
                
                Spacer()
                
                Button(action: {
                    
                }) {
                    Text("+ 关注")
                        .font(.system(size: 14))
                        .foregroundStyle(Color(hex: "#00E9DA"))
                        .frame(width: 78,height: 30)
                        .background(.clear)
                        .overlay(
                            RoundedRectangle(cornerRadius: 15)
                                .stroke(Color(hex: "#00E9DA"), lineWidth: 0.5)
                        )
                }
                
            }
            .padding(.horizontal,20)
            .padding(.top,20)
            
            // 正文内容
            Text(dynamicitem.content)
                .font(.system(size: 16))
                .foregroundStyle(.white)
                .padding(.horizontal,20)
                .padding(.top,12)
                .lineLimit(isExpanded ? nil : 3)
                .onTapGesture { isExpanded.toggle() }
            
            if let address = dynamicitem.address,!address.isEmpty {
                ZStack {
                    Image("map")
                        .resizable()
                        .frame(height: 74)
                    
                }.padding(.horizontal,20)
            }
            // 九宫格图片
            if !dynamicitem.mediaUrls.isEmpty {
                PostImagesView(images: dynamicitem.mediaUrls)
                    .padding(.horizontal,20)
            }
            // 点赞评论栏
            HStack(spacing: 40) {
                Button(action: {
                    isLike.toggle()
                    if isLike {
                        proModel.dynamicLike(dynamicId: dynamicitem.dynamicId)
                    }else {
                        proModel.dynamicUnlike(dynamicId: dynamicitem.dynamicId)
                    }
                }) {
                    Label("\(dynamicitem.likeCount)", image: isLike ? "like" : "dianzan")
                }
                Button(action: {}) {
                    Label("\(dynamicitem.commentCount)", image: "pinglun")
                }
                Spacer()
                
                if (dynamicitem.userId == Int( UserConfigManager.shared.userId)) {
                    Button(action: {
                        //删除
                        
                    }) {
                        Image("shanchu")
                    }
                }
            }
            .foregroundColor(.white)
            .padding(.horizontal,20)
            .padding(.top,16)
            .padding(.bottom,20)
            .font(.system(size: 16))
            .toast(isPresented: $showToast) {
                ShowMessageView(message: proModel.upError)
            }
            .onChange(of: proModel.upError) {
                newValue in
                if let errorMessage = newValue, !errorMessage.isEmpty {
                    showToast = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
                        showToast = false
                        proModel.upError = nil
                    }
                }
            }
            .onChange(of: proModel.isLikeSuccess) {isSuccess in
                if isSuccess {
                        // 点赞成功的处理逻辑
                        print("操作成功")
                    dynamicitem.likeCount  += isLike ? 1 : -1
                    dynamicitem.liked = isLike
                    proModel.isLikeSuccess = false
                    } else {
                        // 点赞失败的处理逻辑
                        print("操作失败")
                    }
            }
            .onChange(of: proModel.isUnlikeSuccess) {isSuccess in
                if isSuccess {
                        // 点赞成功的处理逻辑
                        print("操作成功")
                    dynamicitem.likeCount  += isLike ? 1 : -1
                    dynamicitem.liked = isLike
                    proModel.isUnlikeSuccess = false
                    } else {
                        // 点赞失败的处理逻辑
                        print("操作失败")
                    }
            }
        }
        .background(Color(hex: "#1E1E1E"))
        .cornerRadius(12)
        .padding(.horizontal,12)
        .onAppear {
            print(dynamicitem.avatar!)
        }
        
    }
}
//#Preview {
//    SportsCircleView()
//}
